//
//  FoundViewController.swift
//  Beadhouse
//
//  Created by Apple on 2018/10/24.
//  Copyright © 2018年 wangdong. All rights reserved.
//

import UIKit
import Alamofire
import HandyJSON
class FoundViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
   
    
    lazy var dataArray = []
    
    let tableView = UITableView(frame:UIScreen.main.bounds, style:.plain)
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor =  UIColor.green
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.estimatedRowHeight = 50
        tableView.rowHeight = UITableView.automaticDimension
        self.view .addSubview(tableView)
       tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "found")
        getData()
        // Do any additional setup after loading the view.
    }
    // MARK: 下载解析数据
    func getData() -> Void {
        
        
        let recommend_collectionurl = "http://capi.douyucdn.cn/api/v1/getbigDataRoom?aid=ios&client_sys=ios&time=1468636740&token=30890623_1b036814902f6451&auth=7d7026a323e09dd55c71ca215fc9d4b2"
        
        Alamofire.request(recommend_collectionurl, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseString { (response) in
            
            if response.result.isSuccess {
                
                if let jsonString = response.result.value {
                    
                    /// json转model
                    /// 写法一：responseModel.deserialize(from: jsonString)
                    if let tesponseModel = FoundModel.deserialize(from: jsonString){
                        tesponseModel.data?.forEach({ (model) in
                            print(model.nickname!);
                            self.dataArray.append(model)
                        })
                        self.tableView .reloadData()
                    }
                    
                    /// 写法二：用JSONDeserializer<T>
//                    if let responseModel = JSONDeserializer<FoundModel>.deserializeFrom(json: jsonString) {
//
//                        /// model转json 为了方便在控制台查看
//                        print(responseModel.toJSONString(prettyPrint: true)!)
//                        /// 遍历responseModel.data
//                        responseModel.data?.forEach({ (model) in
//                            print(model.nickname!);
//                             self.dataArray.append(model)
//                        })
//                          self.tableView .reloadData()
//
//                    }
    
                   
                }
            }
        }
    }
//        let  netWork = NetWorkUtilsSwift()
//
//        netWork .get(geturl: "http://api.liwushuo.com/v2/channels/104/items?ad=2&gender=2&generation=2&limit=20&offset=0", body: [:], headerFile: [:]) { (result, error) in

//            if let dic =  result as? Dictionary<String, Any> {
//
//                if let data = dic["data"] as? Dictionary<String, Any>, let arr = data["items"] as? Array<Dictionary<String, Any>> {
//                    print(arr)
//                    self.dataArray = [HomeMoel].deserializeFrom(array: arr) ?? []
//                    self.tableView.reloadData()
//                }
//            }
//        }
        
 //       Alamofire.request("http://api.liwushuo.com/v2/channels/104/items?ad=2&gender=2&generation=2&limit=20&offset=0").responseJSON {
 //           (response)   in

//            if let dic = response.result.value as? Dictionary<String, Any> {
//
//                if let data = dic["data"] as? Dictionary<String, Any>, let arr = data["items"] as? Array<Dictionary<String, Any>> {
//                    print(arr)
//                    self.dataArray = [HomeMoel].deserializeFrom(array: arr) ?? []
//                    self.tableView.reloadData()
//                }
//
//            }
            
        //if let object = FoundModel.deserialize(from: response) {
 //               print(response.result.value)

       // }
  //  }
//}
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier:"found", for: indexPath)
        
        let model:TagModel = self.dataArray[indexPath.row] as! TagModel
        //let testModel:TagModel  = model.data![TagModel]
        cell.textLabel?.text = model.nickname
        return cell
        
    }
   

}
